<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>4.发布留言案例</title>
    <style>
        .box {
            width: 300px;
            /* border: 1px solid red; */
            margin: 100px auto;

        }

        .clearfix::after,
        .clearfix::before {
            content: '';
            display: table;
            clear: both;
        }

        .area {
            position: absolute;
        }

        .area button {
            position: relative;
            top: -6px;

        }

        ul>li {
            font-size: 14px;
            line-height: 20px;
            color: blue;
            background-color: #bfa;
            margin: 5px 0;
            margin-left: -40px;
            list-style-type: none;
            width: 287px;
            height: 20px;
        }

        li a {
            float: right;
            text-decoration: none;
        }
    </style>
</head>

<body>
    <div class="box clearfix">
        <div class="area ">
            <textarea name="" id="" cols="30" rows="10"></textarea>
            <button>发布</button>
            <ul>

            </ul>
        </div>
        <a href=""></a>


    </div>

    <script>
        var text = document.querySelector('textarea');
        var btn = document.querySelector('button');
        var ul = document.querySelector('ul');

        btn.onclick = function () {
            if (text.value == '') {
                alert("你没有输入内容！");
                return false;
            }
            else {
                // 每次点击都创建一个元素
                var li = document.createElement('li');
                //把文本框的内容赋值给li
                li.innerHTML = text.value + "<a href = 'javascript:;'>删除</a>";
                //头部添加的方法添加li
                // ul.appendChild(li);
                ul.insertBefore(li, ul.children[0]);
                //清空文本框内容
                text.value = '';
                //获取链接a
                var links = ul.querySelectorAll('a');
                console.log(links);
                //为每一个链接注册事件
                for (let index = 0; index <links.length; index++) {
                   links[index].onclick=function(){
                       //去除链接所在的li
                       ul.removeChild(ul.children[index]);
                   }
                   
               }
            }
        }
    </script>
    
</body>

</html>